<template>
     <el-card style="max-width: 480px;margin: auto;margin-top: 400px;">
  <el-form
    ref="ruleFormRef"
    style="max-width: 600px"
    :model="ruleForm"
    :rules="rules"
    label-width="auto"
  >
    <el-form-item label="账号" prop="UserName">
      <el-input v-model="ruleForm.UserName" />
    </el-form-item>
   <el-form-item label="密码" prop="UserPwd">
      <el-input v-model="ruleForm.UserPwd" show-password/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)">
        登录
      </el-button>
      <el-button @click="resetForm(ruleFormRef)">取消</el-button>
    </el-form-item>
  </el-form>
  </el-card>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import axios from 'axios'
import { useRouter } from 'vue-router'
const router=useRouter();
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'

import {useCounterStore} from '@/stores/counter'
const pinia=useCounterStore();
import { Read,Write } from '@/http/interceptors'




const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive({
  UserName: '',
  UserPwd: '',
})

const locationOptions = ['Home', 'Company', 'School']

const rules = reactive<FormRules>({
  UserName: [
    { required: true, message: '账号不能为空', trigger: 'blur' },
  ],
  UserPwd: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
  ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
      Read.get("/api/LoginUserLogin",{params:ruleForm}).then(res=>{
        if(res.data.code==200)
        {
            localStorage.setItem('token',res.data.data.token);
            console.log('token:',localStorage.getItem('token'))
            pinia.account.UserId=res.data.data.userId
            pinia.account.UserName=res.data.data.userName
            pinia.account.RoleId=res.data.data.roleId
            pinia.account.RoleName=res.data.data.roleName
            pinia.account.Token=res.data.data.token
            ElMessage.success(res.data.message)
            router.push('/')
        }
        else{
            ElMessage.error(res.data.message)
        }
      })
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

</script>
